//
//  BlockViewModel.swift
//  W_BAR_MAIN
//
//  Created by LC_WJH on 2017/11/2.
//  Copyright © 2017年 LC_WJH. All rights reserved.
//

import UIKit



/* 典型用法
 
  let b = BlockViewModel();
  b.workWithTalbe(table, estimateHeight: 20);  // 列表受控,并确定了一个基础高度,要注意使用了自动高度,cell的自动布局需要能确定高度
  b.table_cell = {
    (t:UITableView,i:IndexPath)->UITableViewCell in
    return t.dequeueReusableCell(withIdentifier: "cell")!;
  }
 
  b.reloadData();
 
  不再需要为viewcontroller单独实现tableviewdelegate和datasource协议
 
 */

/// 使用block来定义tableview和Collectionview行为的功能类,类似于rxswift的思想
/// 其目的非常明确,把对一个列表的各种操作和数据源,封装到一个功能类里面,这样修改对一个列表的行为就变成对这个功能类属性的修改

///暂时这个类还没有移动到库里面,因为还没有十分完善,放在这里方便继续添加各种属性.
public class BlockViewModel: BaseViewModel {

    //table cell
    public typealias TableCellForRowAtIndexPathBlock = (UITableView,IndexPath)->UITableViewCell;
    public var table_cell:TableCellForRowAtIndexPathBlock?;
    
    //TableNumSection
    public typealias TableNumRowInSection = (UITableView,Int) -> Int;
    public var table_numRowInSection:TableNumRowInSection?;
    
    //TableNumRowInSection
    public typealias TableNumberOfSections = (UITableView) -> Int;
    public var table_numberOfSections:TableNumberOfSections?;
    
    //ViewForHeaderInSection
    public typealias TableViewForHeaderInSection = (UITableView,Int) -> UIView?;
    public var table_viewForHeaderInSection:TableViewForHeaderInSection?;
    
    //height for header
    public typealias TableHeightForHeaderInSection = (UITableView,Int) -> CGFloat;
    public var table_heightForHeaderInSection:TableHeightForHeaderInSection?;
    
    
    //table didselect / diddeselect
    public typealias TableViewSelectUnselect = (UITableView,IndexPath) -> Void;
    public var table_didSelectRow:TableViewSelectUnselect?;
    public var table_didDeSelectRow:TableViewSelectUnselect?;
    
    
    public typealias TableViewCanEditRow = (UITableView,IndexPath) -> Bool?;
    public var table_canEditRow:TableViewCanEditRow?;
    
    //col cell
    public typealias CollectionCellForRowAtIndexPathBlock = (UICollectionView,IndexPath)->UICollectionViewCell;
    public var collection_cell:CollectionCellForRowAtIndexPathBlock?;
    
    //col size
    public typealias CollectionCellSizeBlock = (UICollectionView,UICollectionViewLayout,IndexPath)->CGSize;
    public var collection_size:CollectionCellSizeBlock?;
    
    //col insets
    public typealias CollectionSectionInsetsBlock = (UICollectionView,UICollectionViewLayout,Int)->UIEdgeInsets;
    public var collection_insets:CollectionSectionInsetsBlock?;
    

    
    public override func workWithTalbe(_ table: UITableView, estimateHeight: CGFloat) {
        super.workWithTalbe(table, estimateHeight: estimateHeight);
    }
    

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return  collection_insets != nil ? collection_insets!(collectionView,collectionViewLayout,section) : UIEdgeInsets.zero;
    }
    
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return collection_size != nil ? collection_size!(collectionView,collectionViewLayout,indexPath) : expectItemSize;
        
    }
    
    public override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let c = collection_cell?(collectionView,indexPath) {return c;}
        return collectionView.dequeueReusableCell(withReuseIdentifier: "dequeueReusableCell出错!", for: indexPath);
    }
    
    
    override public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell =  table_cell?(tableView,indexPath){return cell;}
        return tableView.dequeueReusableCell(withIdentifier: "dequeueReusableCell出错!")!;
    }
    
    public override func numberOfSections(in tableView: UITableView) -> Int {
        
        if table_numberOfSections != nil
        {return table_numberOfSections!(tableView);}
        
        return super.numberOfSections(in: tableView);
    }
    
    public override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        if table_heightForHeaderInSection != nil {
            return table_heightForHeaderInSection!(tableView,section);
        }
        
        return super.tableView(tableView, heightForHeaderInSection: section);
    }
    
    public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if table_viewForHeaderInSection != nil
        {return table_viewForHeaderInSection!(tableView,section);}
        return nil;
    }
    
    
    public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return table_numRowInSection != nil ? table_numRowInSection!(tableView,section) : ListItems.count;
        
    }
    
    public override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if table_didSelectRow != nil {
            table_didSelectRow!(tableView,indexPath);
            return;
        }
        super.tableView(tableView, didSelectRowAt: indexPath);
    }
    
    public override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        if table_didDeSelectRow != nil {
            table_didDeSelectRow!(tableView,indexPath);
            return;
        }
        super.tableView(tableView, didDeselectRowAt: indexPath);
    }
    
    public override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return super.tableView(tableView, canEditRowAt: indexPath);
    }
   
    
    
}
